package com.ttsx.controller;

import com.ttsx.pojo.dto.product.ProductQueryDTO;
import com.ttsx.pojo.entity.Product;
import com.ttsx.service.ProductService;
import com.ttsx.core.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wb
 * @since 2025-03-11
 */

@RestController
@RequestMapping("product")
public class ProductController {


    @Autowired
    private ProductService productService;

    @PostMapping("page/{pIndex}/{pSize}")
    public Result<?> page(@PathVariable("pIndex") Integer pIndex, @PathVariable("pSize") Integer pSize, @RequestBody ProductQueryDTO productQueryDTO){
        return Result.success(productService.getPage(pIndex, pSize, productQueryDTO));
    }


    @GetMapping("get/{id}")
    public Result<?> getById(@PathVariable("id") Long id) {
        return Result.success(productService.getById(id));
    }

    @PostMapping("save")
    public Result<?> save(@RequestBody Product product) {
        return productService.save(product) ? Result.success() : Result.failure();
    }

    @PutMapping("update")
    public Result<?> update(@RequestBody Product product) {
        return productService.updateById(product) ? Result.success() : Result.failure();
    }

    @DeleteMapping("remove/{id}")
    public Result<?> delete(@PathVariable("id") Long id) {
        return productService.removeById(id) ? Result.success() : Result.failure();
    }

    @DeleteMapping("batchRemove")
    public Result<?> batchDelete(@RequestBody List<Long> ids) {
        return productService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }
}
